package com.woke.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.woke.CreditinfoActivity;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.adapter.Myzcaiadter;
import com.woke.data.Data_zcaiall;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class All_ptFragment4 extends Fragment implements OnItemClickListener{
	private View layout;
	private LayoutInflater minflater;
//	private ImageView mBar;
	private RelativeLayout mRelativ;
	private MyApp myapp;
	private ListView mListView;
	private String getuserid = "";
	private ArrayList<Data_zcaiall> datalist1 = new ArrayList<Data_zcaiall>();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if(layout == null){
			minflater = inflater;
			myapp = (MyApp) getActivity().getApplication();
			layout = minflater.inflate(R.layout.item_singel_listview, null);
			intview();
		}else{
			ViewGroup parent = (ViewGroup) layout.getParent();
			if(parent!=null){
				parent.removeView(layout);
			}
		}
		return layout;
	}

	private void intview() {
		mListView = (ListView) layout.findViewById(R.id.listview_singel);
		mListView.setOnItemClickListener(this);
		mRelativ = (RelativeLayout) layout.findViewById(R.id.fmmyloan_frame);
		if(myapp.getDatas_load ()!=null){
			getuserid  = myapp.getDatas_load().getId();
			getmyloandata1();
		}
	}
	
	private void getmyloandata1() {
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(getActivity());    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "quota");
		params.put("a", "getPlatform");
		params.put("user_id", getuserid);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			private Myzcaiadter adapter;

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("tijiao***", "**"+response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						JSONObject jsonObject = response.getJSONObject("data");
						JSONArray arra = jsonObject.getJSONArray("data");
						for (int i = 0; i < arra.length(); i++) {
							JSONObject data = arra.getJSONObject(i);
							String id = data.getString("id");
							String user_id = data.getString("user_id");
							String type = data.getString("type");
							String interest = data.getString("interest");
							String quota_money = data.getString("quota_money");
							String quota_use = data.getString("quota_use");
							String services_fee = data.getString("services_fee");
							String repay_date = data.getString("repay_date");
							String creat_date = data.getString("creat_date");
							String status = data.getString("status");
							String mark = data.getString("mark");
							String exp_date = data.getString("exp_date");
							String quota_type = data.getString("quota_type");
							String friend = data.getString("friend");
							String quota_pic = data.getString("quota_pic");
							String quota_pic1 = data.getString("quota_pic1");
							String quota_pic2 = data.getString("quota_pic2");
							String flag1 = data.getString("flag");
							String publish = data.getString("publish");
//							String avatar = data.getString("avatar");
//							String money = data.getString("money");
							if(status.equals("4")){
								Data_zcaiall list = new Data_zcaiall(id,user_id,type,interest
										,quota_money,quota_use,services_fee, 
										repay_date,creat_date, status,mark,
										exp_date, quota_type,
										friend,quota_pic,quota_pic1,quota_pic2,flag1,publish);
								datalist1.add(list);
							}
							
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
				if(datalist1.size() > 0){
					adapter = new Myzcaiadter(minflater, datalist1);
					mListView.setAdapter(adapter);
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(getActivity(), "数据获取失败！", Toast.LENGTH_SHORT).show();
				Log.e("response", "失败"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mRelativ.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mRelativ.setVisibility(View.GONE);
//				Log.e("response", "���");
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(getActivity(),CreditinfoActivity.class);
		intent.putExtra("id", datalist1.get(arg2).id);
		intent.putExtra("user_id", datalist1.get(arg2).user_id);
		startActivity(intent);
	}
	
}
